#include "aliot_ota.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "cJSON.h"

//生成推送版本主题
int32_t version_post(char** dest,char *product_key, char *device_name)
{
    int totalSize = strlen("/ota/device/inform//") + strlen(product_key) + strlen(device_name) + 1;
    *dest = (char*)malloc(totalSize);
    if(!*dest)
        return 0;
    snprintf(*dest,totalSize,"/ota/device/inform/%s/%s",product_key,device_name);
    return totalSize - 1;
}

//生成推送版本payload
int32_t version_json(char** dest,int id,const char* version)
{
    cJSON * root =  cJSON_CreateObject();
    cJSON * paramObj = cJSON_CreateObject();
    char s_id[9];
    snprintf(s_id,9,"%d",id);
    cJSON_AddStringToObject(root, "id", s_id);
    cJSON_AddStringToObject(paramObj, "version", version);
    //cJSON_AddStringToObject(paramObj, "module", "MCU");
    cJSON_AddItemToObject(root, "params", paramObj);
    
    *dest = cJSON_Print(root);
    cJSON_Delete(root);
    return strlen(*dest);
}

//生成订阅固件升级信息主题
int32_t ota_info_sub(char** dest,char *product_key, char *device_name)
{
    int totalSize = strlen("/ota/device/upgrade//") + strlen(product_key) + strlen(device_name) + 1;
    *dest = (char*)malloc(totalSize);
    if(!*dest)
        return 0;
    snprintf(*dest,totalSize,"/ota/device/upgrade/%s/%s",product_key,device_name);
    return totalSize - 1;
}

//生成推送升级进度主题
int32_t ota_process_post(char** dest,char *product_key, char *device_name)
{
    int totalSize = strlen("/ota/device/progress//") + strlen(product_key) + strlen(device_name) + 1;
    *dest = (char*)malloc(totalSize);
    if(!*dest)
        return 0;
    snprintf(*dest,totalSize,"/ota/device/progress/%s/%s",product_key,device_name);
    return totalSize - 1;
}

//生成升级进度payload
int32_t ota_process_json(char** dest,int id,int step)
{
    cJSON * root =  cJSON_CreateObject();
    cJSON * paramObj = cJSON_CreateObject();
    char s_id[9];
    snprintf(s_id,9,"%d",id);
    cJSON_AddStringToObject(root, "id", s_id);
    snprintf(s_id,9,"%d",step);
    cJSON_AddStringToObject(paramObj, "step", s_id);
    cJSON_AddStringToObject(paramObj, "desc", "");
    cJSON_AddStringToObject(paramObj, "module", "MCU");
    cJSON_AddItemToObject(root, "params", paramObj);
    
    *dest = cJSON_Print(root);
    cJSON_Delete(root);
    return strlen(*dest);
}

//生成主动拉取升级信息主题
int32_t ota_request_topic(char** dest,char *product_key, char *device_name)
{
	///ota/device/request/a1W2Uba2nEW/${deviceName}
	
	int totalSize = strlen("/ota/device/request//") + strlen(product_key) + strlen(device_name) + 1;
    *dest = (char*)malloc(totalSize);
    if(!*dest)
        return 0;
    snprintf(*dest,totalSize,"/ota/device/request/%s/%s",product_key,device_name);
    return totalSize - 1;
}

//生成主动拉取升级信息payload
int32_t ota_request_payload(char** dest,int id)
{
	cJSON * root =  cJSON_CreateObject();
    cJSON * paramObj = cJSON_CreateObject();
    char s_id[9];
    snprintf(s_id,9,"%d",id);
    cJSON_AddStringToObject(root, "id", s_id);
    cJSON_AddStringToObject(paramObj, "module", "MCU");
    cJSON_AddItemToObject(root, "params", paramObj);
    
    *dest = cJSON_Print(root);
    cJSON_Delete(root);
    return strlen(*dest);
}

uint8_t byte_to_hex(char b)
{
    if(b>='A' && b <= 'Z')
    {
        return b-'A'+10;
    }
    else if(b >= 'a' && b <= 'z')
    {
        return b-'a'+10;
    }
    else if(b>= '0' && b <= '9')
    {
        return b-'0';
    }
    return 0;
}

//字符HEX转HEX
int32_t str_to_hex(char* str,uint8_t hex[],int len)
{
    int i = 0;
    int hexLen = 0;
    int str_len = strlen(str);
    for(i = 0;i < str_len && hexLen < len;i+=2,hexLen++)
    {
        hex[hexLen] = (byte_to_hex(str[i])<<4);
        if(i + 1 < str_len)
            hex[hexLen] |= byte_to_hex(str[i+1]);
    }
    return hexLen;
}
